package richbar.com.richbar.activity.me;

import android.view.View;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.IsBandBean;
import richbar.com.richbar.bean.UserInfoResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by QiYang on 2018/11/16.
 * 账号关联
 */

public class AccountRelationAc extends BaseActivity {
    @BindView(R.id.wx_status)
    TextView wxStatus;
    @BindView(R.id.qq_status)
    TextView qqStatus;
    private LoadingDialog loadingDialog;
    public static boolean isBand=false;
    @Override
    public int initLayout() {
        return R.layout.ac_account_relation;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("账号关联");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this, 0);
    }

    @Override
    public void initData() {
        loadingDialog.show();
        NetWorkUtil.IsBand(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                loadingDialog.dismiss();
                final IsBandBean isBandBean = MyGson.getInstance().fromJson(data, IsBandBean.class);
                if (isBandBean.getData().getRes() == 1) {
                    wxStatus.setText("未关联");
                } else if (isBandBean.getData().getRes() == 2) {
                    wxStatus.setText("已关联");
                }
                ToastUtil.showToast(AccountRelationAc.this, isBandBean.getMsg());
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                loadingDialog.dismiss();
                ToastUtil.showToast(AccountRelationAc.this, msg);
            }
        });
    }

    @OnClick({R.id.wx_body, R.id.qq_body})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wx_body:
                if (wxStatus.getText().toString().equals("未关联")) {
                    loginWx();
                } else if (wxStatus.getText().toString().equals("已关联")) {
                    ToastUtil.showToast(AccountRelationAc.this, "已经关联微信");
                }
                break;
            case R.id.qq_body:
                break;
        }
    }

    private void loginWx() {
        if (BaseApplication.iwxapi.isWXAppInstalled()) {
            LogUtil.e("LLLLALLLAL", "1");
            SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "wechat_sdk_fth";//
            req.state = "wechat_sdk_xb_live_state";
            BaseApplication.iwxapi.sendReq(req);
            isBand=true;
        } else {
            LogUtil.e("LLLLALLLAL", "2");
            ToastUtil.showToast(this, "您还没有安装微信");
        }
    }
}
